import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dio/dio.dart';
import 'package:flutter_hi_cache/flutter_hi_cache.dart';
import 'package:yysk_app/pages/login/login_controller.dart';
import 'package:yysk_app/utils/js_tool.dart';

class ErrorInterceptors extends InterceptorsWrapper {
  @override
  void onRequest(
    RequestOptions options,
    RequestInterceptorHandler handler,
  ) async {
    // 没有网络
    var connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult.contains(ConnectivityResult.none)) {
      return handler.reject(
        DioException(
          requestOptions: options,
          type: DioExceptionType.unknown,
          response: Response(requestOptions: options),
        ),
      );
    }
    
    var token =
        await HiCache.getInstance().get(LoginController.boardingPassToken) ??
            getToken();
    // var mallToken = HiCache.getInstance().get(MallPageController.mallToken)??''; //MallPageController
    options.headers['token'] = '$token';
    // options.headers['accesstoken'] = mallToken;
    return handler.next(options);
  }
}
